package com.inqbarna.tablefixheaders.samples.adapters;

import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;
import com.inqbarna.tablefixheaders.samples.ResourceTable;
import com.inqbarna.tablefixheaders.utils.ResUtils;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * This class implements the main functionalities of the TableAdapter in
 * Mutuactivos.
 *
 * @author Brais Gabín
 */

public abstract class SampleTableAdapter extends BaseTableAdapter {

    private final Context context;

    private final LayoutScatter inflater;

    public SampleTableAdapter(Context context) {
        this.context = context;
        inflater = LayoutScatter.getInstance(context);
    }

    /**
     * Returns the context associated with this array adapter. The context is
     * used to create views from the resource passed to the constructor.
     *
     * @return The Context associated with this adapter.
     */

    public Context getContext() {
        return context;
    }

    /**
     * Quick access to the LayoutInflater instance that this Adapter retreived
     * from its Context.
     *
     * @return The shared LayoutInflater.
     */
    public LayoutScatter getInflater() {
        return inflater;
    }

    @Override
    public Component getView(int row, int column, Component convertView, ComponentContainer parent) {
        if (convertView == null) {
            int layoutRes = getLayoutResource(row, column);
            convertView = inflater.parse(layoutRes, parent, false);
            switch (layoutRes) {
                case ResourceTable.Layout_item_table1_header:
                    convertView.findComponentById(ResourceTable.Id_container)
                        .setBackground(ResUtils.getShapeElement(context, ResUtils.ANGLE_270,
                            ResourceTable.Color_item_table1_header_start_color,
                            ResourceTable.Color_item_table1_header_end_color));
                    break;
                case ResourceTable.Layout_item_table1:
                    convertView.findComponentById(ResourceTable.Id_container)
                        .setBackground(ResUtils.getShapeElement(context, ResUtils.ANGLE_270,
                            ResourceTable.Color_item_table1_start_color, ResourceTable.Color_item_table1_end_color));
                    break;
                default:
                    throw new RuntimeException("");
            }
        }
        setText(convertView, getCellString(row, column));
        return convertView;
    }

    private void setText(Component view, String text) {
        ((Text) view.findComponentById(ResourceTable.Id_text1)).setText(text);
    }

    /**
     * Gets cell string.
     *
     * @param row the title of the row of this header. If the column is -1
     * returns the title of the row header.
     * @param column the title of the column of this header. If the column is -1
     * returns the title of the column header.
     * @return the string for the cell [row, column]
     */

    public abstract String getCellString(int row, int column);

    public abstract int getLayoutResource(int row, int column);
}
